/*
 * Copyright (C) 2012 Yichao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import ezcms.client.UserServiceDelegate;
import ezcms.dto.User;
import ezcms.exceptions.LookupException;
import ezcms.exceptions.PresentationException;

/**
 *
 * @author Yichao
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class CreateUserAction extends ActionSupport {

    UserServiceDelegate userServiceDelegate;
    private String name;
    private String password;
    private String password1;
    private String email;
    private String address;

    public CreateUserAction() throws PresentationException {
        try {
            userServiceDelegate = new UserServiceDelegate();
        } catch (LookupException ex) {
            throw new PresentationException(ex);
        }
    }

    @Override
    public String execute() throws Exception {
        User user = new User();
        user.setFirstName(getName());
        user.setPassword(getPassword());
        user.setEmail(getEmail());
        user.setAddress(getAddress());
        User oldUser = userServiceDelegate.getUser(getEmail());
        if (oldUser.getFirstName().equals(getName())) {
            return "error";
        }
        userServiceDelegate.saveUser(user);
        return "success";
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getPassword1() {
        return password1;
    }

    public String getEmail() {
        return email;
    }

    public String getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
